package com.junyuan.fxf_pad.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.junyuan.fxf_pad.R;
import com.junyuan.fxf_pad.application.MyApplication;

import java.util.ArrayList;

public class VoicePlayListActivity extends AppCompatActivity {
    int musicCount;
    ArrayList<String> musicList;
    ListView listView;
    ArrayAdapter adapter;
    VoicePlayListBroadCastReceiver voicePlayListBroadCastReceiver;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            adapter = new ArrayAdapter(VoicePlayListActivity.this, R.layout.item_voice, musicList);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d("mytag", "点击了" + position + "位置");
                    String song = "";

                    if (position < 10) {
                        song = "00" + (position + 1);
                    } else {
                        song = "0" + (position + 1);
                    }
                    if (MyApplication.instance.mBleService != null) {
//                        try {
//                            MyApplication.instance.mBleService.writeCharacteristic
//                                    (MyApplication.instance.mCharacteristic, "000");
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                        MyApplication.instance.mBleService.writeCharacteristic
                                (MyApplication.instance.mCharacteristic, song);
                        Log.d("mytag", "点击了" + song);
                    }
                }
            });


            return false;
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        voicePlayListBroadCastReceiver = new VoicePlayListBroadCastReceiver();
        IntentFilter filter = new IntentFilter("BackData");
        registerReceiver(voicePlayListBroadCastReceiver, filter);

        setContentView(R.layout.activity_voice_play_list);


        listView = (ListView) findViewById(R.id.lvVoice_Play_List);

    }

    class VoicePlayListBroadCastReceiver extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {
            String backData = intent.getStringExtra("BackData");//18
            Log.d("mytag", "MoreMusicActivity的广播接收到：" + backData);
            musicCount = Integer.parseInt(backData);
            musicList = new ArrayList<>();
            musicList = generMusicList(musicCount);
            handler.sendEmptyMessage(0);
        }
    }

    public ArrayList<String> generMusicList(int arg) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= arg; i++) {
            list.add("第" + i + "首歌");
        }
        return list;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(voicePlayListBroadCastReceiver);
    }
}
